package com.baijiayun.live.ui.base

import android.text.TextUtils
import com.baijiayun.live.ui.activity.LiveRoomRouterListener
import com.baijiayun.live.ui.isAdmin
import com.baijiayun.live.ui.menu.rightmenu.RightMenuContract
import com.baijiayun.live.ui.pptpanel.MyPadPPTView
import com.baijiayun.live.ui.speakerlist.item.RemoteItem
import com.baijiayun.live.ui.speakerlist.item.Switchable
import com.baijiayun.livecore.context.LPConstants
import com.baijiayun.livecore.context.LPError
import com.baijiayun.livecore.context.LiveRoom
import com.baijiayun.livecore.models.LPAwardUserInfo
import com.baijiayun.livecore.models.LPBJTimerModel
import com.baijiayun.livecore.models.LPJsonModel
import com.baijiayun.livecore.models.LPRedPacketModel
import com.baijiayun.livecore.models.imodels.IUserModel
import com.baijiayun.livecore.models.launch.LPAwardConfig
import com.baijiayun.livecore.ppt.PPTView
import com.baijiayun.liveuibase.base.BaseUIConstant
import com.baijiayun.liveuibase.widgets.toolbar.ShapeChangeData
import java.util.*

/**
 * Created by Shubo on 2019-10-16.
 */
class OldLiveRoomRouterListenerBridge(val routerViewModel: RouterViewModel) : LiveRoomRouterListener {

    init {
        routerViewModel.routerListener = this
    }
    override fun getLiveRoom(): LiveRoom {
        return routerViewModel.liveRoom
    }

    override fun switchToFullScreen(switchable: Switchable?, isFullScreen: Boolean) {
        routerViewModel.switch2FullScreen.value = isFullScreen to switchable
    }

    override fun switchToMainScreen(switchable: Switchable?) {
        routerViewModel.switch2MainVideo.value = switchable
    }

    override fun switchToMaxScreen(switchable: Switchable?) {
        if (switchable?.view is MyPadPPTView) {
            routerViewModel.closeDrawingMode.value = Unit
        }
        routerViewModel.switch2MaxScreen.value = switchable
    }

    override fun switchToSpeakList(switchable: Switchable?) {
        routerViewModel.switch2SpeakList.value = switchable
    }

    override fun notifyPageCurrent(position: Int) {
        routerViewModel.notifyPPTPageCurrent.value = position
    }

    override fun navigateToPPTDrawing(isAllowDrawing: Boolean) {
        navigateToPPTDrawing(ShapeChangeData(BaseUIConstant.SelectSrc.Brush,
                if (isAllowDrawing) LPConstants.PPTEditMode.ShapeMode else LPConstants.PPTEditMode.Normal, LPConstants.ShapeType.Doodle))
    }

    override fun navigateToPPTDrawing(shapeChangeData: ShapeChangeData) {
        routerViewModel.actionNavigateToPPTDrawing.value = shapeChangeData
    }

    override fun getMainScreenItem() = routerViewModel.mainVideoItem

    override fun setPPTShowType(type: LPConstants.LPPPTShowWay?) {
        routerViewModel.pptViewData.value?.pptShowWay = type
    }

    override fun addPPTWhiteboardPage() {
        routerViewModel.addPPTWhiteboardPage.value = Unit
    }

    override fun deletePPTWhiteboardPage(pageId: Int) {
        routerViewModel.deletePPTWhiteboardPage.value = pageId
    }

    override fun changePage(docId: String?, pageNum: Int) {
        routerViewModel.changePPTPage.value = Pair(docId, pageNum)
    }

    override fun isTeacherOrAssistant(): Boolean = liveRoom.isTeacherOrAssistant

    override fun isGroupTeacherOrAssistant(): Boolean = liveRoom.isGroupTeacherOrAssistant


    override fun attachLocalVideo() {
        routerViewModel.actionAttachLocalVideo.value = true
    }

    override fun attachLocalAudio() {
        routerViewModel.actionAttachLocalAudio.value = true
    }

    override fun detachLocalVideo() {
        routerViewModel.actionAttachLocalVideo.value = false
    }

    override fun attachLocalAVideo() {
        routerViewModel.actionAttachLocalAVideo.value = true
    }

    override fun sendImageMessage(path: String?) {
        routerViewModel.sendPictureMessage.value = path
    }

    override fun dismissCustomWebpage() {
        routerViewModel.actionCloseWebpage.value = ""
    }

    override fun showSavePicDialog(bmpArray: ByteArray?) {
        routerViewModel.showSavePicDialog.value = bmpArray
    }

    override fun realSaveBmpToFile(bmpArray: ByteArray?) {
        routerViewModel.saveChatPictureToGallery.value = bmpArray
    }

    override fun showError(error: LPError?) {
        // do nothing
    }

    override fun canStudentDraw(): Boolean {
        return isAdmin(routerViewModel.liveRoom) || routerViewModel.pptViewData.value?.isCurrentMaxPage  ?: false
    }

    override fun isCurrentUserTeacher(): Boolean = liveRoom.currentUser.type == LPConstants.LPUserType.Teacher

    override fun getSpeakApplyStatus(): Int = routerViewModel.speakApplyStatus.value
            ?: RightMenuContract.STUDENT_SPEAK_APPLY_NONE

    override fun getMaxScreenItem(): Switchable? = routerViewModel.switch2MaxScreen.value

    override fun getPPTView(): PPTView? = routerViewModel.pptViewData.value

    override fun dismissQuizDlg() {
        routerViewModel.quizStatus.postValue(RouterViewModel.QuizStatus.CLOSE to LPJsonModel())
    }

    override fun showToast(message: String?) {
        routerViewModel.actionShowToast.postValue(message)
    }

    override fun showDebugBtn() {
        // do nothing
    }

    override fun onRepublishRedPacket() {
        routerViewModel.redPacketPublish.value = Unit
    }

    override fun onPrivateChatUserChange(iUserModel: IUserModel?) {
        routerViewModel.privateChatUser.value = iUserModel
    }

    override fun getPrivateChatUser(): IUserModel? = routerViewModel.privateChatUser.value

    override fun isPrivateChat(): Boolean = routerViewModel.privateChatUser.value != null

    override fun enableAnimPPTView(b: Boolean): Boolean = routerViewModel.pptViewData.value?.setAnimPPTEnable(b)
            ?: false

    override fun setRemarksEnable(isEnable: Boolean) {
        routerViewModel.remarkEnable.value = isEnable
    }

    override fun switchRedPacketUI(isShow: Boolean, lpRedPacketModel: LPRedPacketModel?) {
        routerViewModel.action2RedPacketUI.value = isShow to lpRedPacketModel
    }

    override fun dismissEvaDialog() {
        routerViewModel.showEvaDlg.postValue(false)
    }

    override fun closeTimer() {
        routerViewModel.showTimer.value = false to LPBJTimerModel()
    }

    override fun requestAward(userModel: IUserModel?) {
        requestAward(userModel, LPAwardConfig.AWARD_TYPE_KEY_PRAISE)
    }

    override fun requestAward(userModel: IUserModel?, key: String) {
        //不可点赞
        if (!routerViewModel.canAward(userModel)) {
            return
        }
        val userNumber = userModel?.number?:""
        if(TextUtils.isEmpty(userNumber)){
            return
        }
        val awardRecord = routerViewModel.awardRecord
        var userInfo = awardRecord[userNumber]
        if(userInfo == null){
            val typeCount: MutableMap<String, Int> = getTypeCount()
            typeCount[key] = 1
            userInfo = LPAwardUserInfo(1, typeCount)
        } else{
            userInfo.count++
            if (userInfo.typeCount == null) {
                userInfo.typeCount = getTypeCount()
            }
            val keyCount: Int? = userInfo.typeCount[key]
            if (keyCount == null) {
                userInfo.typeCount[key] = 1
            } else {
                userInfo.typeCount[key] = keyCount + 1
            }
        }
        userInfo.name = userModel?.name
        awardRecord[userNumber] = userInfo
        routerViewModel.liveRoom.requestAward(userNumber, key, awardRecord)
    }

    private fun getTypeCount(): MutableMap<String, Int> {
        val map: MutableMap<String, Int> = HashMap()
        val awardConfigs: Array<LPAwardConfig> = liveRoom.awardConfigs
        for (lpAwardConfig in awardConfigs) {
            if (lpAwardConfig.isEnable == 1) {
                map[lpAwardConfig.key] = 0
            }
        }
        return map
    }

    override fun handleUserCloseAction(remoteItem: RemoteItem?) {
        routerViewModel.notifyCloseRemoteVideo.value = remoteItem
    }

    override fun showBleDialog() {
        routerViewModel.actionShowBleDialog.value = Unit
    }
}